//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CDO.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
#include <gen/QL/Experimental/Credit/OneFactorCopula.h>
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CCDO::CCDO (Double attachment, Double detachment, Cephei::IVector<Double>^ nominals, Cephei::IVector<Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^>^ basket, Cephei::QL::Experimental::Credit::IOneFactorCopula^ copula, Boolean protectionSeller, Cephei::QL::Times::ISchedule^ premiumSchedule, Double premiumRate, Cephei::QL::Times::IDayCounter^ dayCounter, Double recoveryRate, Double upfrontPremiumRate, Cephei::QL::Termstructures::IYieldTermStructure^ yieldTS, UInt64 nBuckets, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ integrationStep, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CCDO::typeid)
{
    CoVector<Double>^ _Cnominals;
    CoVector<Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^>^ _Cbasket;
    COneFactorCopula^ _Ccopula;
    CSchedule^ _CpremiumSchedule;
    CDayCounter^ _CdayCounter;
    CYieldTermStructure^ _CyieldTS;
    CPeriod^ _CintegrationStep;
    try
    {
#ifdef HANDLE
        _phCDO = NULL;
#endif
        QuantLib::Real _attachment = (QuantLib::Real)ValueHelper::Convert (attachment);
        QuantLib::Real _detachment = (QuantLib::Real)ValueHelper::Convert (detachment);
        CoVector<Double>^ _Cnominals = safe_cast<CoVector<Double>^> (nominals);
        _Cnominals->Lock();
        INativeVector<Double>^ _NCInominals = _Cnominals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCnominals = safe_cast<CDoubleVector^>(_NCInominals);
        std::vector<QuantLib::Real>& _nominals = static_cast<std::vector<QuantLib::Real>&> (_NCnominals->GetReference ());
        _Cbasket = safe_cast<CoVector<Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^>^> (basket);
        _Cbasket ->Lock ();
        INativeVector<Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^>^ _NCIbasket = _Cbasket->getFeature (NativeFeature::Handle);
        CDefaultProbabilityTermStructureVector^ _NCbasket = safe_cast<CDefaultProbabilityTermStructureVector^>(_NCIbasket);
        std::vector<Handle<QuantLib::DefaultProbabilityTermStructure> >& _basket = static_cast<std::vector<Handle<QuantLib::DefaultProbabilityTermStructure> >&> (_NCbasket->GetHandle ());
        _Ccopula = safe_cast<COneFactorCopula^> (copula);
        _Ccopula->Lock();
        Handle<QuantLib::OneFactorCopula>& _copula = static_cast<Handle<QuantLib::OneFactorCopula>&> (_Ccopula->GetHandle ()); 
        bool _protectionSeller = (bool)ValueHelper::Convert (protectionSeller);
        _CpremiumSchedule = safe_cast<CSchedule^> (premiumSchedule);
        _CpremiumSchedule->Lock();
        QuantLib::Schedule& _premiumSchedule = static_cast<QuantLib::Schedule&> (_CpremiumSchedule->GetReference ()); 
        QuantLib::Rate _premiumRate = (QuantLib::Rate)ValueHelper::Convert (premiumRate);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Rate _recoveryRate = (QuantLib::Rate)ValueHelper::Convert (recoveryRate);
        QuantLib::Rate _upfrontPremiumRate = (QuantLib::Rate)ValueHelper::Convert (upfrontPremiumRate);
        _CyieldTS = safe_cast<CYieldTermStructure^> (yieldTS);
        _CyieldTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _yieldTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CyieldTS->GetHandle ()); 
        QuantLib::Size _nBuckets = (QuantLib::Size)ValueHelper::Convert (nBuckets);
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (integrationStep))
        {
            _CintegrationStep = safe_cast<CPeriod^> (integrationStep->Value);
            _CintegrationStep->Lock();
        }
        QuantLib::Period& _integrationStep = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (integrationStep) ? static_cast<QuantLib::Period&> (_CintegrationStep->GetReference ()) : QuantLib::Period(10, Years)); //1
        _ppCDO = new boost::shared_ptr<QuantLib::CDO> (new QuantLib::CDO ( _attachment,  _detachment,  _nominals,  _basket,  _copula,  _protectionSeller,  _premiumSchedule,  _premiumRate,  _dayCounter,  _recoveryRate,  _upfrontPremiumRate,  _yieldTS,  _nBuckets,  _integrationStep ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCDO)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCDO));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cnominals != nullptr) _Cnominals->Unlock();
        if (_Cbasket != nullptr) _Cbasket->Unlock();
        if (_Ccopula != nullptr) _Ccopula->Unlock();
        if (_CpremiumSchedule != nullptr) _CpremiumSchedule->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_CyieldTS != nullptr) _CyieldTS->Unlock();
        if (_CintegrationStep != nullptr) _CintegrationStep->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CCDO::CCDO (boost::shared_ptr<QuantLib::CDO>& childNative, Object^ owner) : CInstrument(CCDO::typeid)
{
#ifdef HANDLE
	_phCDO = NULL;
#endif
	_ppCDO = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCDO));
}
Cephei::QL::Experimental::Credit::CCDO::CCDO (QuantLib::CDO& childNative, Object^ owner) : CInstrument(CCDO::typeid)
{
#ifdef HANDLE
	_phCDO = NULL;
#endif
	_ppCDO = new boost::shared_ptr<QuantLib::CDO> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCDO));
    _CDOOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Experimental::Credit::CCDO::CCDO (CCDO^ copy) : CInstrument(CCDO::typeid)
{
#ifdef HANDLE
	_phCDO = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCDO = new boost::shared_ptr<QuantLib::CDO> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCDO));
    }
}
Cephei::QL::Experimental::Credit::CCDO::CCDO (System::Type^ t) : CInstrument(CCDO::typeid)
{
#ifdef HANDLE
	_phCDO = NULL;
#endif
	if (!t->IsSubclassOf(CCDO::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CCDO::CCDO (QuantLib::Handle<QuantLib::CDO>& childNative, Object^ owner)  : CInstrument(CCDO::typeid)
{
	_phCDO = &childNative;
	_ppCDO = &static_cast<boost::shared_ptr<QuantLib::CDO>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCDO));
    _CDOOwner = owner;
}
Cephei::QL::Experimental::Credit::CCDO::CCDO (QuantLib::Handle<QuantLib::CDO> childNative)  : CInstrument(CCDO::typeid)
{
	_phCDO = &childNative;
	_ppCDO = &static_cast<boost::shared_ptr<QuantLib::CDO>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCDO));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CCDO::CCDO (QuantLib::CDO childNative)  : CInstrument(CCDO::typeid)
{
#ifdef HANDLE
	_phCDO = NULL;
#endif
	_ppCDO = new boost::shared_ptr<QuantLib::CDO> (new QuantLib::CDO (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCDO));
}
#endif

Cephei::QL::Experimental::Credit::CCDO::~CCDO ()
{
    if (_ppCDO != NULL)
    {
	    delete _ppCDO;
        _ppCDO = NULL;
    }
}
Cephei::QL::Experimental::Credit::CCDO::!CCDO ()
{
    if (_ppCDO != NULL)
    {
	    delete _ppCDO;
    }
}
QuantLib::CDO& Cephei::QL::Experimental::Credit::CCDO::GetReference ()
{
    if (_ppCDO == NULL) throw gcnew NativeNullException ();
	return **_ppCDO;
}
boost::shared_ptr<QuantLib::CDO>& Cephei::QL::Experimental::Credit::CCDO::GetShared ()
{
    if (_ppCDO == NULL) throw gcnew NativeNullException ();
	return *_ppCDO;
}
QuantLib::CDO* Cephei::QL::Experimental::Credit::CCDO::GetPointer ()
{
    if (_ppCDO == NULL) throw gcnew NativeNullException ();
	return &**_ppCDO;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CDO>& Cephei::QL::Experimental::Credit::CCDO::GetHandle ()
{
	if (_phCDO == NULL)
	{
		_phCDO = new Handle<QuantLib::CDO> (*_ppCDO);
	}
	return *_phCDO;
}
#endif
bool Cephei::QL::Experimental::Credit::CCDO::HasNative () 
{
	return (_ppCDO != NULL);
}

Double Cephei::QL::Experimental::Credit::CCDO::Attachment::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCDO)->attachment ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CCDO::Detachment::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCDO)->detachment ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Experimental::Credit::CCDO::Error::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppCDO)->error ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CCDO::FairPremium::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCDO)->fairPremium ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Credit::CCDO::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCDO)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CCDO::Lgd::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCDO)->lgd ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CCDO::Nominal::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCDO)->nominal ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Experimental::Credit::CCDO::Nominals::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	std::vector<QuantLib::Real> _rv = (std::vector<QuantLib::Real>)(*_ppCDO)->nominals ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CCDO::PremiumValue::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCDO)->premiumValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CCDO::ProtectionValue::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCDO)->protectionValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Experimental::Credit::CCDO::Size::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppCDO)->size ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::ICDO^ Cephei::QL::Experimental::Credit::CCDO_Factory::Create (Double attachment, Double detachment, Cephei::IVector<Double>^ nominals, Cephei::IVector<Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^>^ basket, Cephei::QL::Experimental::Credit::IOneFactorCopula^ copula, Boolean protectionSeller, Cephei::QL::Times::ISchedule^ premiumSchedule, Double premiumRate, Cephei::QL::Times::IDayCounter^ dayCounter, Double recoveryRate, Double upfrontPremiumRate, Cephei::QL::Termstructures::IYieldTermStructure^ yieldTS, UInt64 nBuckets, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ integrationStep, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CCDO ( attachment,  detachment,  nominals,  basket,  copula,  protectionSeller,  premiumSchedule,  premiumRate,  dayCounter,  recoveryRate,  upfrontPremiumRate,  yieldTS,  nBuckets,  integrationStep,  QL_Pricer);
}
